//
//  TTBeautyManagerEditViewController.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/30.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "TTBeautyManagerEditViewController.h"
#import "TTBeautyManagerCell.h"
#import "ButtomView.h"
#import "Pet.h"
#import "PetServiceViewController.h"

@interface TTBeautyManagerEditViewController ()<UITableViewDataSource, UITableViewDelegate, ButtomViewDelegate, TTBeautyManagerCellDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) ButtomView *buttomView;
@property (nonatomic, assign, getter=isAllSelected) BOOL allSelected;

@end

@implementation TTBeautyManagerEditViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"编辑美容服务";
    
    [self setUI];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(editDidComplete)];
}

- (void)setUI {
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.rowHeight = 100;
    
    ButtomView *buttomView = [ButtomView buttomViewWithLeftTitle:@"全选" rightTitle:@"删除"];
    buttomView.backgroundColor = [UIColor clearColor];
    buttomView.delegate = self;
    [self.view addSubview:buttomView];
    self.buttomView = buttomView;
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.tableView.width = self.view.width;
    self.tableView.height = self.view.height - 44;
    
    self.buttomView.width = self.view.width;
    self.buttomView.height = 49;
    self.buttomView.y = self.view.height - self.buttomView.height;
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TTBeautyManagerCell *cell = [TTBeautyManagerCell beautyManagerCellWithTableView:tableView];
    cell.editStatus = YES;
    cell.pet = self.dataSource[indexPath.row];
    cell.tag = indexPath.row;
    cell.delegate = self;
    return cell;
    
}

#pragma mark - TTBeautyManagerCellDelegate
- (void)beautyManagerCell:(TTBeautyManagerCell *)cell didSelectedEditButton:(Pet *)pet {
    self.dataSource[cell.tag] = pet;
    NSInteger selectedCount = 0;
    for (Pet *pet in self.dataSource) {
        if (pet.selected == YES) {
            selectedCount++;
        }
    }
    if (selectedCount) {
        self.buttomView.rightTitle = [NSString stringWithFormat:@"删除(%zd)", selectedCount];
    }else {
        self.buttomView.rightTitle = @"删除";
    }
}

#pragma mark - ButtomViewDelegate
- (void)buttomView:(ButtomView *)buttomView didSelectedButtomViewType:(ButtomViewType)type {
    NSMutableArray *indexPathes = nil;
    NSMutableArray *tempDataSource = nil;
    switch (type) {
        case ButtomViewTypeLeft:
            self.allSelected = !self.allSelected;
            for (Pet *pet in self.dataSource) {
                pet.selected = self.allSelected;
                
            }
            if (self.allSelected ) {
                self.buttomView.rightTitle = [NSString stringWithFormat:@"删除(%zd)", self.dataSource.count];
            }else {
                self.buttomView.rightTitle = @"删除";
            }
            [self.tableView reloadData];
            break;
        case ButtomViewTypeRight:
            indexPathes = [NSMutableArray array];
            tempDataSource = [NSMutableArray array];
            [self.dataSource enumerateObjectsUsingBlock:^(Pet *pet, NSUInteger idx, BOOL *stop) {
                if (pet.selected == YES) {
                    [tempDataSource addObject:pet];
                    [indexPathes addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
                }
            }];
            [self.dataSource removeObjectsInArray:tempDataSource];
            [self.tableView beginUpdates];
            [self.tableView deleteRowsAtIndexPaths:indexPathes withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView endUpdates];
            break;
    }
}

#pragma mark - UIButton Action
- (void)editDidComplete {
    
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[PetServiceViewController class]]) {
            PetServiceViewController *petServiceVc = (PetServiceViewController *)vc;
            petServiceVc.dataSource = self.dataSource;
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
